/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.actions.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Results;

import com.javainfo.entity.User;
import com.javainfo.exception.AppException;
import com.javainfo.simpledto.ArticleCategoryDto;
import com.javainfo.simpledto.UserDto;
import com.javainfo.util.JavaInfoActionSupport;

@Results( {})
public class AdminCtrl extends JavaInfoActionSupport{

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	@Override
	public String index() throws Exception {
		List<UserDto> users = getServiceFactory().getUserService().getAllUser();
		List<ArticleCategoryDto> categorys = getServiceFactory().getCategoryService().getCategoryList();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("users", users);
		model.put("categorys", categorys);
		setModel(model);
		return "usermanage";
	}
	
	public String userManage(){
		List<UserDto> users = getServiceFactory().getUserService().getAllUser();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("users", users);
		setModel(model);
		return "usermanage";
	}
	
	public String categoryManage(){
		List<ArticleCategoryDto> categorys = getServiceFactory().getCategoryService().getCategoryList();
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("categorys", categorys);
		setModel(model);
		return "categorymanage";
	}
	
	@SuppressWarnings("finally")
	public String deleteUser() {
		User user = new User();
		user.setId(Integer.valueOf(getId()));
		// 判断权限并执行
		try {
			getServiceFactory().getUserService().delete(user);
			addActionMessage("删除用户成功!");
		} catch (AppException e) {
			addActionError(e.getMessage());
		} finally {
			setRedirectUrl("admin");
			return "urlhandler";
		}
	}
}
